package com.wonderful.eyepetizer.view

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.material3.FabPosition
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.unit.dp
import com.google.accompanist.systemuicontroller.SystemUiController
import com.google.accompanist.systemuicontroller.rememberSystemUiController

/**
 * Desc: system bar: https://juejin.cn/post/7233304089371213861
 * @Author: wonderful
 * @Time: 2024/1/19 17:55
 */

@Composable
fun FullScreenScaffold(
    modifier: Modifier = Modifier,
    topBar: @Composable () -> Unit = {},
    bottomBar: @Composable () -> Unit = {},
    snackBarHost: @Composable () -> Unit = {},
    floatingActionButton: @Composable () -> Unit = {},
    floatingActionButtonPosition: FabPosition = FabPosition.End,
    containerColor: Color = MaterialTheme.colorScheme.background,
    contentColor: Color = contentColorFor(containerColor),
    content: @Composable (PaddingValues) -> Unit
) {
    val systemUiController = rememberSystemUiController()
    val isUseDarkModeIcons = shouldUseDarkModeIcons()
    SideEffect {
        transparentSystemBars(systemUiController, isUseDarkModeIcons)
    }
    Scaffold(
        modifier = modifier,
        topBar = topBar,
        bottomBar = bottomBar,
        snackbarHost = snackBarHost,
        floatingActionButton = floatingActionButton,
        floatingActionButtonPosition = floatingActionButtonPosition,
        containerColor = containerColor,
        contentColor = contentColor,
        contentWindowInsets = WindowInsets(0.dp, 0.dp, 0.dp, 0.dp),
        content = content
    )
}

@Composable
fun PageScaffold(
    modifier: Modifier = Modifier,
    statusBarColor: Color = Color.Transparent,
    navigationBarColor: Color = Color.Transparent,
    useDarkModeIcons: (() -> Boolean)? = null,
    topBar: @Composable () -> Unit = {},
    bottomBar: @Composable () -> Unit = {},
    snackBarHost: @Composable () -> Unit = {},
    floatingActionButton: @Composable () -> Unit = {},
    floatingActionButtonPosition: FabPosition = FabPosition.End,
    containerColor: Color = MaterialTheme.colorScheme.background,
    contentColor: Color = contentColorFor(backgroundColor = containerColor),
    content: @Composable (PaddingValues) -> Unit
) {
    val systemUiController = rememberSystemUiController()
    val isUseDarkModeIcons = if (useDarkModeIcons != null) {
        useDarkModeIcons()
    } else {
        shouldUseDarkModeIcons(bgColor = statusBarColor)
    }
    SideEffect {
        setSystemBarsColor(
            systemUiController,
            isUseDarkModeIcons,
            statusBarColor,
            navigationBarColor
        )
    }

    Scaffold(
        modifier = modifier,
        topBar = topBar,
        bottomBar = bottomBar,
        snackbarHost = snackBarHost,
        floatingActionButton = floatingActionButton,
        floatingActionButtonPosition = floatingActionButtonPosition,
        containerColor = containerColor,
        contentColor = contentColor,
        contentWindowInsets = WindowInsets(0.dp, 0.dp, 0.dp, 0.dp),
        content = content
    )
}

@Composable
fun TransparentSystemBar(bgColor: Color = Color.Transparent) {
    val systemUiController = rememberSystemUiController()
    val useDarkIcons = shouldUseDarkModeIcons(bgColor)
    SideEffect {
        transparentSystemBars(systemUiController, useDarkIcons)
    }
}

@Composable
fun SetStatusBarColor(color: Color) {
    val systemUiController = rememberSystemUiController()
    val useDarkIcons = shouldUseDarkModeIcons(color)
    SideEffect {
        setStatusBarColor(systemUiController, useDarkIcons, color)
    }
}

@Composable
fun SetNavigationBarColor(color: Color) {
    val systemUiController = rememberSystemUiController()
    val useDarkIcons = shouldUseDarkModeIcons(color)
    SideEffect {
        setNavigationBarColor(systemUiController, useDarkIcons, color)
    }
}

@Composable
fun SetSystemBarsColor(statusBarColor: Color, navigationBarColor: Color) {
    val systemUiController = rememberSystemUiController()
    val useDarkIcons = shouldUseDarkModeIcons(statusBarColor)
    SideEffect {
        setSystemBarsColor(systemUiController, useDarkIcons, statusBarColor, navigationBarColor)
    }
}

fun transparentSystemBars(systemUiController: SystemUiController, useDarkIcons: Boolean) {
    systemUiController.setSystemBarsColor(
        color = Color.Transparent,
        darkIcons = useDarkIcons,
        isNavigationBarContrastEnforced = false
    )
}

fun setStatusBarColor(
    systemUiController: SystemUiController,
    useDarkIcons: Boolean,
    statusBarColor: Color
) {
    systemUiController.setStatusBarColor(
        color = statusBarColor,
        darkIcons = useDarkIcons
    )
}

fun setNavigationBarColor(
    systemUiController: SystemUiController,
    useDarkIcons: Boolean,
    navigationBarColor: Color
) {
    systemUiController.setNavigationBarColor(
        color = navigationBarColor,
        darkIcons = useDarkIcons,
        navigationBarContrastEnforced = false
    )
}

fun setSystemBarsColor(
    systemUiController: SystemUiController,
    useDarkIcons: Boolean,
    statusBarColor: Color,
    navigationBarColor: Color
) {
    setStatusBarColor(systemUiController, useDarkIcons, statusBarColor)
    setNavigationBarColor(systemUiController, useDarkIcons, navigationBarColor)
}

/**
 * 根据 bgColor 亮度判断 systemBar 中 icon 是否使用 暗色模式
 * @param bgColor systemBar 背景颜色
 * @return true 需要使用暗色 icon, false 亮色模式
 */
@Composable
fun shouldUseDarkModeIcons(bgColor: Color = Color.Transparent): Boolean {
    return if (bgColor == Color.Transparent) {
        !isSystemInDarkTheme()
    } else {
        return bgColor.luminance() >= 0.5
    }
}